package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.HotTopicAdapter.ViewHolder;
import com.ykdl.member.kid.beans.ArticleBean;
import com.ykdl.member.kid.beans.Hot_TopicsBean;
import com.ykdl.member.kid.util.DateUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ArticlesAdapter extends BaseAdapter {

	private static ArrayList<ArticleBean> list;
	private BitmapUtils bitmapUtil;
	private Context mContext;

	public ArticlesAdapter(Context context) {
		mContext = context;
		bitmapUtil = new BitmapUtils(context);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defult_head_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defult_head_icon);
	}

	public void setList(ArrayList<ArticleBean> list) {
		this.list = list;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (list == null) {
			return 0;
		} else {
			return list.size();
		}

	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0 - 1);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = RelativeLayout.inflate(mContext,
					R.layout.articlesadapter_item, null);
			holder = new ViewHolder();
			holder.iv_Lift = (ImageView) convertView.findViewById(R.id.iv_lift);
			holder.iv_Middle = (ImageView) convertView
					.findViewById(R.id.iv_middle);
			holder.tv_Title = (TextView) convertView
					.findViewById(R.id.tv_title);
			holder.tv_Title2 = (TextView) convertView
					.findViewById(R.id.tv_title2);
			holder.tv_Content = (TextView) convertView
					.findViewById(R.id.tv_content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.iv_Middle.setImageResource(0);
		if (list.size() == 1) {
			holder.iv_Middle.setImageResource(R.drawable.iv_middle1);

		} else if (list.size() == 2) {
			if (arg0 == 1) {
				holder.iv_Middle.setImageResource(R.drawable.iv_middle);
			} else if (arg0 == (list.size() - 1)) {
				holder.iv_Middle.setImageResource(R.drawable.iv_middle3);
			}
		} else if (list.size() > 2) {
			if (arg0 == 0) {
				holder.iv_Middle.setImageResource(R.drawable.iv_middle);
			} else if (arg0 > 0 && (arg0 < (list.size() - 1))) {
				Log.i("arg0", arg0 + "");
				Log.i("list.size()", list.size() + "");
				holder.iv_Middle.setImageResource(R.drawable.iv_middle2);
			} else if (arg0 == (list.size() - 1)) {
				holder.iv_Middle.setImageResource(R.drawable.iv_middle3);
			}
			
		}
		String uri = "";
		if (list.get(arg0).getUrl() != null) {
			uri = list.get(arg0).getUrl();
			bitmapUtil.display(holder.iv_Lift, uri);
		} else {
			bitmapUtil.display(holder.iv_Lift, uri);
		}
		holder.tv_Title.setText(list.get(arg0).getBaby_days());
		if (list.get(arg0).getTitle() != null) {
			holder.tv_Title2.setText(list.get(arg0).getTitle());
		}
		if (list.get(arg0).getIntro() != null) {
			holder.tv_Content.setText(list.get(arg0).getIntro());
		}
		return convertView;

	}

	class ViewHolder {
		public ImageView iv_Lift;
		public ImageView iv_Middle;
		public TextView tv_Title;
		public TextView tv_Title2;
		public TextView tv_Content;

	}
}
